package org.example.ttms.controller;

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import org.example.ttms.Main;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.entityClass.Account;
import org.example.ttms.entityClass.Listening;
import org.example.ttms.entityClass.Manager;
import org.example.ttms.entityClass.User;

import java.io.IOException;
import java.util.HashMap;

public class ManagerMessageController {

    @FXML
    private StackPane stackPane3;

    @FXML
    private Button saveButton;

    @FXML
    private Button quitButton;

    @FXML
    private TextField nameTextField;

    @FXML
    private TextField passwordTextField;

    private Manager manager;

    public void initialize(){
        manager = ManagerController.manager;
        if(manager.getName() != null) {
            nameTextField.setText(manager.getName());
        }
        passwordTextField.setText(manager.getPassword());
    }

    @FXML
    void chickQuit(ActionEvent event) {
        try {
            //关闭监听
            Listening listening = new Listening();
            listening.end();
            Main.jumpPage("login.fxml");
            Main.stage.setX(500);
            Main.stage.setY(200);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @FXML
    void chickSave(ActionEvent event) {
        HashMap<String, Manager> hashMap = manager.obtainAllManager();
        Pop_ups popUps = new Pop_ups();
        if(hashMap.get(nameTextField.getText()) != null && !nameTextField.getText().equals(manager.getName())){
            popUps.pop_ups3("该昵称已存在");
            return ;
        }
        Account account = new Account();
        if(account.passwordIsExist(passwordTextField.getText())
                && !passwordTextField.getText().equals(manager.getPassword())){
            popUps.pop_ups3("该密码已存在");
            return ;
        }
        String name = manager.getName();
        manager.setName(nameTextField.getText());
        manager.setPassword(passwordTextField.getText());
        hashMap.put(name, manager);
        manager.storageManager(FXCollections.observableArrayList(hashMap.values()));
        popUps.pop_ups4("保存成功");
    }

}


